@include "lib.awk"
@include "tile.awk"

function _tiles(f,max) {
  print f
  readcsv(f ? f : "data/tiles.dat",datas)
  dump(datas)
}
function tiles(f,max,  datas,all) {
  readnums("cat " f,datas) 
  largefx(rx,all)
  tilesReport(datas,all,max)
}

# function largefx(rx,all,min,max,  \
#                  n,i,some,x,y,z,third,small) {
#   min = min ? min : 0.01
#   max = max ? max : 0.90
#   n = asort(rx[0][0],all)
#   min = int(n*min)
#   max=int(n*max)
#   if(min==0) min =1
#   third = (all[max] - all[min])/3
#   for(i=1;i<= n;i++) {
#     if(all[i] <= third) 
#       some[i] = all[i]
#     else
#       break
#   }
#   small = some[int(i/2)]
#   for(x in rx)
#     for(y in rx[x])
#       for(z in rx[x][y])
#         rx[x][y][z] = \
#           small/2 + small * int(rx[x][y][z]/small) 
#   print "# Small= " small
# }
# function tilesReport(rx,all,max,  min,x,y,com,chops) {
#   com = "sort -t, -n -k 12,13"
#   min = all[1]
#   pairs("0.1,-,0.3, ,0.5, ,0.7,-,0.9, ",chops)
#   for(x in rx)
#     if (x ~ /=/) 
#       printf("%30s,%10s, %s\n",
#              "overall",x, 
#              tile(rx[x][0],chops,50,
#                   "%5s",min,max)) | com
#   close(com)
#   for(x in rx)
#     if (x ~/@/) {
#       print ""
#       for(y in rx[x])
#         printf("%30s,%10s, %s\n",
#                x,y, 
#                tile(rx[x][y],chops,50,
#                     "%5s",min,max)) | com
#       close(com) }
# }

# #rx[data][rx][i]=v

# function scottKnott(data) {
#   for(data in datas)
#     if ( data ~ /@/)  
#       sk1(datas[data])

# function sk1(rxs,_Bag,m1) {
#   sort_rx(rxs,sorted,stats)
# }
# function sort_rx(rxs,sorted,stats) {
#   for(rx in rxs) 
#     for(r in rxs) {
#       v = rxs[rx][r]
#       l[rx][r]      
# }

# function sk_recurse(a,m,_Bag,rank,left,right,   cut,i) {
#   cut = sk_split(a,m,_Bag,left,right)
#   if (! cut)
#     for(i=left;i<=right;i++)
#       r[m[i]] = rank
#   else {
#     rank = sk_recurse(a,m,_Bag,rank,left, cut) + 1
#     rank = sk_recurse(a,m,_Bag,rank,cut+1,right)
#   }
#   return r
# }
# function sk_split(a,m,_Bag,left,right,   cut) {
#   before = -1
#   left  = m[left]
#   for(i=left,i<=right;i++) {
#     if (i==left) continue
#     j = m[i]
#     n1 += n[j]
#     n2  = 
#     n2[i] = n1
#   }
#   return cut
# }
# function reduce(a,m,_Num,lo,hi,   i,hi,lo,x){
#   for(i=lo;i<=hi;i++) {
#     x = a[i]
#     n[i]++
#     s[i]  += x
#     s2[i] += x*x
#     m[i]["x"] = s[i]/n[i]
#     m[i]["id"]= i
#   }       
# }
# function reduce(a,sorted,_Bag,lo,hi,   i,x,lo,hi){
#   lo = lo ? lo : 1
#   hi = hi ? hi : length(a)
#   for(i=lo;i<=hi;i++) {
#     x = a[i]
#     n[i]++
#     s[i]  += x
#     s2[i] += x*x
#     m[i]["x"] = s[i]/n[i]
#     m[i]["id"]= i
#   }  
#   return asort(m,sorted,"xsort")
# }
